<?php
include("common/header.html");
include("common/left.html");
if( $_SESSION['dtype']!="6" && $_SESSION['dtype']!="0"){
   exit('抱歉，只有供应商才能操作');
}
$conf = array();
$sql="SELECT id,fid,appid,appsecret,name,token,hash,encodingAESKey FROM `pft_wx` WHERE fid={$_SESSION['sid']} LIMIT 1";
$GLOBALS['le']->query($sql);
$conf = $GLOBALS['le']->fetch_assoc();
?>
<script language="javascript">
function $_(id){ return document.getElementById(id);}
function radioGuoupInit(radioElementName,checkedForValue){
    var radioObj = document.getElementsByName(radioElementName);
    var radioLength = radioObj.length;
    for (var i = 0; i<radioLength ; i ++){
        if (radioObj[i].value == checkedForValue){
            radioObj[i].checked  = true ;
        }
    }
}

function Ok(){
    var type = "";
    var radioObj = document.getElementsByName("WECHAT_TYPE");
    var radioLength = radioObj.length;
    var typeid;
    for (var i = 0; i<radioLength ; i ++){
        if (radioObj[i].checked){
             type = radioObj[i].value;
             typeid=i;
        }
    }
    var appid = $_("AppIds").value;
    var Appname = $_("Appname").value;
    var AppSecrets = $_("AppSecrets").value;
    var appsecret  = $_("AppSecrets").value;
    var wechatIds  = $_("wechatIds").value;
    var wechatSecrets  = $_("wechatSecrets").value;
    if(appid==''){
            alert('您还尚未填写appid'); 
            return false;
        }
    if(AppSecrets==''){
            alert('您还尚未填写AppSecrets'); 
            return false;
        }
    if(Appname==''){
            alert('您还尚未填写微信公众号名称'); 
            return false;
        }
    if(type=="service" || type == "service_cert"){
        if(appid.replace(/[ ]/g,"")==""||appsecret.replace(/[ ]/g,"")==""){
            $.dialog.alert("请正确输入AppID与AppSecret信息！",'','',true,'',true);
            return false;
        }
    }
    
    var user = $('input[name=wechatIds]').val(),
        pass = $('input[name=wechatSecrets]').val(),
		action = $("input[name='action']").val();
        var data = {
            'typeid' : typeid,
            'wechatIds' : wechatIds,
            'wechatSecrets' : wechatSecrets,
            'appid' : appid,
            'AppSecrets' : AppSecrets,
            'Appname' : Appname,
			'action' :  action
        };
    $.ajax({
            url : "module/wechat/bind.php",
            type : "POST",
            dataType : "JSON",
            data : data,
            success : function(res){
              if (res.status== 'ok') {
					alert(res.msg);
					window.location.reload(); 
					$("#token_d").val(res.token);
					$("#url_d").val(res.url);
					$("#encodingAESKey").val(res.encodingAESKey);
					$_("service_span_a").style.display = "block";
					$_("wechat_btn").style.display = "none";
              } 
			  else if (res.status== 'fail') {
                    alert(res.msg);
              }
			  else if (res.status== 'failq') {
                    alert(res.msg);
              }
            }
		})
	}
function sure(){
    var appid = $_("AppIds").value;
	var Appname = $_("Appname").value;
    var AppSecrets = $_("AppSecrets").value;
    var appsecret  = $_("AppSecrets").value;
   
    if(appid==''){
            alert('您还尚未填写appid'); 
            return false;
        }
    if(AppSecrets==''){
            alert('您还尚未填写AppSecrets'); 
            return false;
        }
   
   
    
    var action = $("input[name='action']").val();
        var data = {
            'appsecret' :appsecret,
            'appid' : appid,
            'AppSecrets' : AppSecrets,
			'Appname' : Appname,
			'action' :  action
        };
    $.ajax({
            url : "module/wechat/bind.php",
            type : "POST",
            dataType : "JSON",
            data : data,
            success : function(res){
              if (res.status== 'ok') {
				  alert(res.msg);
				  window.location.reload(); 
				  $("#token_d").val(res.token);
				  $("#url_d").val(res.url);
				  $("#encodingAESKey").val(res.encodingAESKey);
				  $_("service_span_a").style.display = "block";
              } else if (res.status== 'fail') {
                    alert(res.msg);
              }
            }
		})
	}
</script>
</head>

<body>
<link rel="stylesheet" type="text/css" href="css/wechat_t.css">
<div class="r_content" id="bind_tao">    
<h3 class="mc_title">
    <span class="mctit_1"><span class="mctit_2">微信公众号配置</span></span>
</h3>
<div class="con_content"> 
    <form action="./call/p.php" id="p03Form" method="post">
		<input type="hidden" value="<?=$conf['fid']? 'update':'insert'?>" name="action"/> 
        <div class="user_msg" style="width:700px;">
            <ul>
                <div class="wx_kuang" style="background-color:#FFFABC">
                    <li style=" padding-left:25%; padding: 30px;font-size: 16px;color:red;">请勿关闭微信公众平台的开发者模式，否则以下配置无法生效&nbsp;&nbsp;&nbsp;<a href="wechat_tutorial.html" target="_blank" class="lnk_help">查看帮助文档</a></li>
                </div> 
                <li><b>公众号类型：</b>
                <label title="服务号">
                <input type="radio" name="WECHAT_TYPE" id="WECHAT_TYPE" value="service" checked="checked" style="height:20px; width:30px"> <img src="http://www.12301.cc/images/fwh.png"></label>
                </li>
               
                <li>请输入您的微信公众平台账号密码，进行验证</li>
                 <form>
                    <div class="wx_kuang">
                    <li><b>账号：</b><input type="text" name="wechatIds" id="wechatIds" style="width:60%" value="<?=$conf['username']?>"></li>
                    <li><b>密码：</b><input type="password" name="wechatSecrets" id="wechatSecrets" style="width:60%" value=""></li>
                    <li class="yan" style="display:none">
                    <b>验证码:</b><input type="text" class="yan_z" name="code" />
                    <img src="" id="imgcode" class="img_a" /><a href="javascript:;" id="changCode">换一张</a>
                    </li>
                    <input type="button" class="submit s_e" id="s_e" value="验证账号"/>
                </form>
               
                 <span id="service_span" style="display: <?=$conf['id']? 'block':'none'?>;">
                 <li>登录您的微信公众号后台，将微信公众账号中<span>信息复制</span>到本网站 （开发者中心&gt;开发者ID）</li>
                <div class="wx_kuang">
                    <input type="hidden" name="action" value="insert"/>
                    <li><b>AppID：</b><input type="text" name="AppIds" id="AppIds" style="width:60%" value="<?=$conf['appid']?>"></li>
                    <li><b>AppSecret：</b><input type="text" name="AppSecrets" id="AppSecrets" style="width:60%" value="<?=$conf['appsecret']?>"></li>
                    <li><b>微信公众号名称：</b><input type="text" name="Appname" id="Appname" style="width:60%" value="<?=$conf['name']?>"></li>
                </div>
                </span>

                <span id="service_span_a" style="display: <?=$conf['id']? 'block':'none'?>;">
                <li>登录您的微信公众号后台，将下面<span>信息复制</span>到微信公众账号中 （开发者中心&gt;服务器配置）</li>
                    <div class="wx_kuang">
                        <p><b>URL：</b><input type="text" class="highlight" id="url_d" value="<?=isset($conf['hash'])? 'http://wx.12301.cc/api.php?hash='.$conf['hash']:''?>"/></p>
                        <p><b>Token：</b><input type="text" class="highlight" id="token_d" value="<?=$conf['token']?>"></p>
                        <p><b>encodingAESKey：</b><input type="text" class="highlight" id="encodingAESKey" value="<?=$conf['encodingAESKey']?>" /></p>
                    </div> 
                </span> 
            </ul>
            <div class="dz_btn_bottom1" id="wechat_btn" style="display: <?=$conf['id']? 'block':'none'?>;">
            <?php if(!$conf['id']){?>
                <a href="javascript:void(0)" class="dz_btn1" onclick="Ok()">保存</a>
            <?}else{?>                        
                <a href="javascript:void(0)" class="dz_btn1" onclick="sure()">确定修改</a>
            <?}?>
            </div>
        </div>
    <script type="text/javascript">
$(function(){
    $('.submit').on('click', function(){
        var user = $('input[name=wechatIds]').val(),
            pass = $('input[name=wechatSecrets]').val();
        if(user==''){
            alert('您还尚未填写账号'); 
            return false;
        }
        if(pass==''){
            alert('您还尚未填写密码');
            return false;
        }
        var data = {
            'user' : user,
            'pass' : pass
        };
        if ( $('.img_a').attr('src') != '' ) {
            var code = $('input[name=code]').val();
            data.code = code;
        }
        $.post('module/wechat/wx_login.php', data, function(res){
            var oRes = JSON.parse(res);
            var wechatId = document.getElementById("wechatIds");
            var wechatSecret = document.getElementById("wechatSecrets");
            //console.log(oRes);
            // -8 需要验证码
            if ( oRes.base_resp.ret == '-8') {
                $('.img_a').attr('src','module/wechat/code.php?u='+user);
                $('.yan').show();
            } else if( oRes.base_resp.ret == 0 ){
                wechatId.setAttribute("readOnly",'true');
                wechatSecret.setAttribute("readOnly",'true');
                $_("service_span").style.display = "block";
                $_("wechat_btn").style.display = "block";
                $_("s_e").style.display = "none";
                $_("wechatIds").style.backgroundColor = "#e5e5e5";
                $_("wechatSecrets").style.backgroundColor = "#e5e5e5";
               
            } else {
                alert('绑定失败');
            }
        });
       
        return false;
        
    });
    $('#changCode').on('click', function(){
        var user = $('input[name=user]').val();
        $('.img_a').attr('src','module/wechat/code.php?u='+user+'&s='+Math.random(1));
    }); 
});

</script>
    </form>
</div>
</div>

<?include 'common/footer.html';?>